package cn.cloudplug.core.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.cloudplug.core.R;
import me.yokeyword.fragmentation.ExtraTransaction;
import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportFragmentDelegate;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * Created by pc on 2018/2/4.
 */

public abstract class BaseFragmentDialog extends AppCompatDialogFragment implements ISupportFragment {
    private final SupportFragmentDelegate SUPPORT = new SupportFragmentDelegate(this);
    protected Activity _mActivity=null;
    private Unbinder mUnbinder =null;

    protected abstract Object setContent();

    protected abstract void onBindView(Bundle savedInstanceState, View view);

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setStyle(STYLE_NO_TITLE,R.style.dialog);
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Object o=setContent();
        View view=null;
        if(o instanceof Integer){
            view=inflater.inflate((Integer) o,container,false);
        }else if(o instanceof View){
            view= (View) o;
        }else{
            throw new ClassCastException("type of setLayout() must be int or View!");
        }
        mUnbinder= ButterKnife.bind(this,view);
        onBindView(savedInstanceState,view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    //不用管
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        SUPPORT.onAttach((Activity) context);
        _mActivity=SUPPORT.getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SUPPORT.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SUPPORT.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        SUPPORT.onSaveInstanceState(outState);
    }


    @Override
    public void onResume() {
        super.onResume();
        SUPPORT.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        SUPPORT.onPause();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        SUPPORT.onHiddenChanged(hidden);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        SUPPORT.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public SupportFragmentDelegate getSupportDelegate() {
        return SUPPORT;
    }

    @Override
    public ExtraTransaction extraTransaction() {
        return SUPPORT.extraTransaction();
    }

    @Override
    public void enqueueAction(Runnable runnable) {
        SUPPORT.enqueueAction(runnable);
    }

    @Override
    public void post(Runnable runnable) {
        SUPPORT.post(runnable);
    }

    @Override
    public void onEnterAnimationEnd(@Nullable Bundle savedInstanceState) {
        SUPPORT.onEnterAnimationEnd(savedInstanceState);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        SUPPORT.onLazyInitView(savedInstanceState);
    }

    @Override
    public void onSupportVisible() {
        SUPPORT.onSupportVisible();
    }

    @Override
    public void onSupportInvisible() {
        SUPPORT.onSupportInvisible();
    }

    @Override
    public boolean isSupportVisible() {
        return SUPPORT.isSupportVisible();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return SUPPORT.onCreateFragmentAnimator();
    }

    @Override
    public FragmentAnimator getFragmentAnimator() {
        return SUPPORT.getFragmentAnimator();
    }

    @Override
    public void setFragmentAnimator(FragmentAnimator fragmentAnimator) {
        SUPPORT.setFragmentAnimator(fragmentAnimator);
    }

    @Override
    public void setFragmentResult(int resultCode, Bundle bundle) {
        SUPPORT.setFragmentResult(resultCode, bundle);
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        SUPPORT.onFragmentResult(requestCode, resultCode, data);
    }

    @Override
    public void onNewBundle(Bundle args) {
        SUPPORT.onNewBundle(args);
    }

    @Override
    public void putNewBundle(Bundle newBundle) {
        SUPPORT.putNewBundle(newBundle);
    }

    @Override
    public boolean onBackPressedSupport() {
        return SUPPORT.onBackPressedSupport();
    }
}
